package com.example.demo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ExecutorTry {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executor= Executors.newFixedThreadPool(2);
//        executor.submit(()->{
//
//            System.out.println(Thread.currentThread().getName()+":Runnable method is running!");
//        });
//        Future<String> future=executor.submit(()->{
//            System.out.println(Thread.currentThread().getName()+":Callable method is running!");
//            return "任务执行完成";
//        });
//        System.out.println(future.get());
//        executor.shutdown();
        List<Callable<Integer>> tasks=new ArrayList<>();
        tasks.add(()->{
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName()+":is running!");
            return 42;
        });
        tasks.add(()->{
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName()+":is running!");
            return 43;
        });
       List<Future<Integer>> r =executor.invokeAll(tasks);
       r.stream().forEach(item-> {
           try {
               System.out.println(item.get());
           } catch (InterruptedException e) {
               e.printStackTrace();
           } catch (ExecutionException e) {
               e.printStackTrace();
           }
       });
       executor.shutdown();
    }
}
